using MongoDB.Driver;
using Sustainalytics.Entities;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace Sustainalytics.Universes.Synchronization
{
    public class AssureFeatureUniverseByCompanyIdHandler<T> : AssureFeatureUniverseBy<T> where T : IHasUniverseIds, IHasCompanyId
    {
        public AssureFeatureUniverseByCompanyIdHandler(string connectionString)
            : base(connectionString)
        {
        }

        protected override async Task<UpdateResult> ClearUniverseIdsAsync()
        {
            return await MongoCollection.UpdateManyAsync(x => true, Builders<T>.Update.Set(x => x.UniverseIds, new List<int>())).ConfigureAwait(false);
        }

        protected override async Task AddUniverseIdsAsync(Dictionary<Guid, List<int>> companyUniverses)
        {
            foreach (var universeIds in companyUniverses)
            {
                var ids = universeIds;
                await MongoCollection.UpdateManyAsync(x => x.CompanyId == ids.Key, Builders<T>.Update.Set(x => x.UniverseIds, universeIds.Value)).ConfigureAwait(false);
            }
        }
    }
}
